﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace ListLimiter
{
    public static class Common
    {
        public static bool GetLimitEnabled(SPWeb web, Guid listId)
        {
            string limitEnabledString = GetWebPropertyValue(web, GetLimitEnabledKeyName(listId));
            bool limitEnabled = false;

            if (!string.IsNullOrEmpty(limitEnabledString))
            {
                limitEnabled = Convert.ToBoolean(limitEnabledString);
            }

            return limitEnabled;
        }

        public static void SetLimitEnabled(SPWeb web, Guid listId, bool limitEnabled)
        {
            SetWebPropertyValue(web, GetLimitEnabledKeyName(listId), limitEnabled.ToString());
        }

        public static int GetLimit(SPWeb web, Guid listId)
        {
            string limitString = GetWebPropertyValue(web, GetLimitKeyName(listId));
            int limit = 0;

            if (!string.IsNullOrEmpty(limitString))
            {
                limit = Convert.ToInt32(limitString);
            }

            return limit;
        }

        public static int GetLimitWarning(SPWeb web, Guid listId)
        {
            int limit = GetLimit(web, listId);
            double warningDouble = limit / 2;
            int warning = Convert.ToInt32(Math.Round(warningDouble, 0));
            return warning;
        }

        public static void SetLimit(SPWeb web, Guid listId, int limit)
        {
            SetWebPropertyValue(web, GetLimitKeyName(listId), limit.ToString());
        }

        public static bool GetLimitStatusEnabled(SPWeb web, Guid listId)
        {
            string statusEnabledString = GetWebPropertyValue(web, GetLimitStatusEnabledKeyName(listId));
            bool statusEnabled = false;

            if (!string.IsNullOrEmpty(statusEnabledString))
            {
                statusEnabled = Convert.ToBoolean(statusEnabledString);
            }

            return statusEnabled;
        }

        public static void SetLimitStatusEnabled(SPWeb web, Guid listId, bool statusEnabled)
        {
            SetWebPropertyValue(web, GetLimitStatusEnabledKeyName(listId), statusEnabled.ToString());
        }

        public static string GetLimitStatusTitle(SPWeb web, Guid listId, bool parsed)
        {
            string statusTitle = GetWebPropertyValue(web, GetLimitStatusTitleKeyName(listId));

            if (parsed)
            {
                statusTitle = ParseStatus(statusTitle, web, listId);
            }

            return statusTitle;
        }

        public static void SetLimitStatusTitle(SPWeb web, Guid listId, string title)
        {
            SetWebPropertyValue(web, GetLimitStatusTitleKeyName(listId), title);
        }

        public static string GetLimitStatusText(SPWeb web, Guid listId, bool parsed)
        {
            string statusText = GetWebPropertyValue(web, GetLimitStatusTextKeyName(listId));
            
            if (parsed)
            {
                statusText = ParseStatus(statusText, web, listId);
            }

            return statusText;
        }



        public static void SetLimitStatusText(SPWeb web, Guid listId, string text)
        {
            SetWebPropertyValue(web, GetLimitStatusTextKeyName(listId), text);
        }

        public static string GetLocalizedString(SPWeb web, string resourceKey)
        {
            string resourceFileName = "ListLimiterResources";
            string key = string.Format("$Resources:{0}", resourceKey);
            return SPUtility.GetLocalizedString(key, resourceFileName, Convert.ToUInt32(web.Locale.LCID));
        }

        private static string GetLimitKeyName(Guid listId)
        {
            return GetKeyName("ListLimit", listId);
        }

        private static string GetLimitEnabledKeyName(Guid listId)
        {
            return GetKeyName("ListLimitEnabled", listId);
        }

        private static string GetLimitStatusTitleKeyName(Guid listId)
        {
            return GetKeyName("ListLimitStatusTitle", listId);
        }

        private static string GetLimitStatusTextKeyName(Guid listId)
        {
            return GetKeyName("ListLimitStatusText", listId);
        }

        private static string GetLimitStatusEnabledKeyName(Guid listId)
        {
            return GetKeyName("ListLimitStatusEnabled", listId);
        }

        private static string GetKeyName(string keyNameFormat, Guid listId)
        {
            return string.Format("{0}_{1}", keyNameFormat, listId.ToString());
        }

        private static string GetWebPropertyValue(SPWeb web, string propertyName)
        {
            string propertyValue = string.Empty;

            if (PropertyExists(web, propertyName))
            {
                propertyValue = web.Properties[propertyName];
            }

            return propertyValue;
        }

        private static void SetWebPropertyValue(SPWeb web, string propertyName, string propertyValue)
        {
            if (PropertyExists(web, propertyName))
            {
                web.Properties[propertyName] = propertyValue;
            }
            else
            {
                web.Properties.Add(propertyName, propertyValue);
            }

            web.Properties.Update();
            web.Update();
        }

        private static bool PropertyExists(SPWeb web, string propertyName)
        {
            return web.Properties.ContainsKey(propertyName);
        }

        private static string ParseStatus(string text, SPWeb web, Guid listId)
        {
            int itemLimit = GetLimit(web, listId);
            int itemCount = web.Lists[listId].ItemCount;
            int itemLeft= itemLimit - itemCount;

            string tempString = text.Replace("[ItemCount]", itemCount.ToString());
            tempString = tempString.Replace("[ItemLimit]", itemLimit.ToString());
            tempString = tempString.Replace("[ItemLeft]", itemLeft.ToString());

            return tempString;
        }
    }
}
